package tobsn.locationcalendar;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import tobsn.locationcalendar.db.DBAdapter;
import tobsn.locationcalendar.db.DBOpenHelper;
import tobsn.locationcalendar.map.overlays.EditPONOverlay;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NotificationDetailActivity extends MapActivity {

	public static final String EXTRA_ID = "EXTRA_ID";

	private MapView mMapView;
	private EditPONOverlay mEditOverlay;
	private EditText mEditText;
	private Button mSaveButton;
	private Button mDeleteButton;
	private DBAdapter mDBAdapter;
	private int currentId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.notificationdetail);
		mMapView = (MapView) findViewById(R.id.detailmapview);
		mEditText = (EditText) findViewById(R.id.detailEditText);
		mSaveButton = (Button) findViewById(R.id.detailSaveButton);
		mDeleteButton = (Button) findViewById(R.id.detailDeleteButton);

		mSaveButton.setOnClickListener(mSaveListener);
		mDeleteButton.setOnClickListener(mDeleteListener);

		mDBAdapter = new DBAdapter(this);

		currentId = getIntent().getIntExtra(EXTRA_ID, 0);
		mDBAdapter.open();
		Cursor c = mDBAdapter.fetchPON(currentId);
		mEditText.setText(c.getString(DBOpenHelper.TEXT_INT));

		Drawable notificationDrawable = this.getResources().getDrawable(
				R.drawable.droid);
		GeoPoint p = new GeoPoint(c.getInt(DBOpenHelper.LATITUDE_INT),
				c.getInt(DBOpenHelper.LONGITUDE_INT));
		mEditOverlay = new EditPONOverlay(notificationDrawable, this, p);
		mMapView.getOverlays().add(mEditOverlay);
		mDBAdapter.close();
	}

	private OnClickListener mDeleteListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mDBAdapter.open();
			mDBAdapter.deletePON(currentId);
			mDBAdapter.close();
			finish();
		}
	};

	private OnClickListener mSaveListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			mDBAdapter.open();
			GeoPoint p = mEditOverlay.getPoint();
			int lat = p.getLatitudeE6();
			int lng = p.getLongitudeE6();
			mDBAdapter.updatePON(currentId, mEditText.getText()
					.toString(), lat, lng);
			mDBAdapter.close();
			finish();
		}
	};

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
